"""
Everything related to how display models when accessing some URLs
"""
from django.shortcuts import render_to_response, get_object_or_404
from cassebras.contest.models import Slope, SuccessfullClimb, Competitor, \
                                     Category, Contest

from django import forms
#from django.forms.extras.widgets import CheckboxSelectMultiple

class CompetitorForm(forms.ModelForm):
    """
    A form that will be used to register Successfull Climbs of a 
    Competitor
    """
    class Meta:
        """
        Meta class used to indicate to form which model to refer to
        and which model fields to exclude from the Form
        """
        model = Competitor
        exclude = ('score', 'number')#, 'name', 'surname', 'category')
    climbed_slopes = forms.ModelMultipleChoiceField(
                                    queryset=Slope.objects.all(), 
                                    widget=forms.CheckboxSelectMultiple, 
                                    required=False)

def competitors(request):
    """
    Method called when accessing ./competitors URL
    """
    if request.method == "GET":
        _competitors = Competitor.objects.all()
        return render_to_response(  'contest/competitors.html', 
                                    {'competitors': _competitors})
    
def competitor(request, competitor_id):
    """
    Method called when accessing ./competitors/$competitor_id URL
    """
    _competitor = get_object_or_404(Competitor, number=competitor_id)
    saved = False
    if request.method == 'POST':
        comp_form = CompetitorForm(request.POST, instance=_competitor)
        if comp_form.is_valid():
            for _slope in comp_form.cleaned_data['climbed_slopes']:
                if _slope not in _competitor.climbed_slopes.all():
                    climb = SuccessfullClimb(  competitor=comp_form.instance, 
                                            slope=_slope)
                    climb.save()
            for _slope in _competitor.climbed_slopes.all():
                if _slope not in comp_form.cleaned_data['climbed_slopes']:
                    SuccessfullClimb.objects.get(   slope=_slope, 
                                                    competitor=_competitor
                                                ).delete()

            _competitor.update_score()
            _competitor.name     = comp_form.cleaned_data['name']
            _competitor.surname  = comp_form.cleaned_data['surname']
            _competitor.category = comp_form.cleaned_data['category']
            _competitor.save()
            saved = True
    else:
        comp_form = CompetitorForm(instance=_competitor)
    return render_to_response(  'contest/competitor.html', 
                                { 'competitor_form': comp_form, 
                                  'competitor': _competitor,
                                  'saved': saved}
                             )

def categories(request):
    """
    """
    if request.method == "GET":
        _categories = Category.objects.all()
        return render_to_response(  'contest/categories.html', 
                                    {'categories': _categories})
                                    
def category(request, category_name):
    if request.method == "GET":
        _category = get_object_or_404(Category, name=category_name)
        _ranking = _category.rank()
        return render_to_response(  'contest/category.html', 
                                    { 'ranking': _ranking}
                                 )

def contest(request):
     """
     """
     if request.method == "GET":
         _categories = Category.objects.all()
         return render_to_response(  'contest/contest.html', 
                                     {'categories': _categories})